import SwiftUI
import UIKit // 导入UIKit便于使用TCP相关功能

struct ContentView: View {
    @EnvironmentObject var connectionSettings: ConnectionSettings
    @EnvironmentObject var tcpCarService: TCPCarService
    @Environment(\.presentationMode) var presentationMode
    
    @State private var selectedTab = 0
    @State private var showDisconnectAlert = false
    
    var body: some View {
        TabView(selection: $selectedTab) {
            // 小车控制
            CarControlView()
                .tabItem {
                    Label("控制", systemImage: "gamecontroller")
                }
                .tag(0)
            
            // AI智能控制
            AIControlView()
                .tabItem {
                    Label("AI控制", systemImage: "brain.head.profile")
                }
                .tag(1)
            
            // 目标识别
            ObjectDetectionView()
                .tabItem {
                    Label("识别", systemImage: "person.crop.rectangle")
                }
                .tag(2)
            
            // 传感器数据
            SensorDataView()
                .tabItem {
                    Label("传感器", systemImage: "gauge")
                }
                .tag(3)
            
            // 自动功能
            AutoFunctionsView()
                .tabItem {
                    Label("自动", systemImage: "car")
                }
                .tag(4)
            
            // 设置
            SettingsView(showDisconnectAlert: $showDisconnectAlert)
                .tabItem {
                    Label("设置", systemImage: "gearshape")
                }
                .tag(5)
        }
        .navigationBarTitle(getTabTitle(), displayMode: .inline)
        .navigationBarItems(
            leading: Button(action: {
                showDisconnectAlert = true
            }) {
                HStack {
                    Image(systemName: "power")
                    Text("断开")
                }
                .foregroundColor(.red)
            },
            trailing: HStack {
                Text("已连接")
                    .font(.subheadline)
                    .foregroundColor(.green)
            }
        )
        .alert(isPresented: $showDisconnectAlert) {
            Alert(
                title: Text("断开连接"),
                message: Text("确定要断开与小车的连接吗？"),
                primaryButton: .destructive(Text("断开")) {
                    disconnectAndReturn()
                },
                secondaryButton: .cancel(Text("取消"))
            )
        }
        .onAppear {
            // 请求更新传感器数据
            tcpCarService.requestSensorData()
            
            // 设置定时器，每10秒更新一次传感器数据（从5秒改为10秒，减少网络负担）
            Timer.scheduledTimer(withTimeInterval: 10, repeats: true) { _ in
                if tcpCarService.isConnected {
                    tcpCarService.requestSensorData()
                }
            }
        }
    }
    
    // 获取当前标签页标题
    private func getTabTitle() -> String {
        switch selectedTab {
        case 0:
            return "小车控制"
        case 1:
            return "AI智能控制"
        case 2:
            return "目标识别"
        case 3:
            return "传感器数据"
        case 4:
            return "自动功能"
        case 5:
            return "设置"
        default:
            return "智能小车"
        }
    }
    
    // 断开连接并返回
    private func disconnectAndReturn() {
        // 断开TCP连接
        tcpCarService.disconnect()
        connectionSettings.closeTCPConnection()
        
        // 返回连接界面
        presentationMode.wrappedValue.dismiss()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
            .environmentObject(ConnectionSettings())
            .environmentObject(TCPCarService())
    }
} 
